package com.handydata.antifraud.service.impl;

import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.handydata.antifraud.bo.decisionexecrecord.DecisionExecRecordBeanBO;
import com.handydata.antifraud.bo.decisionexecrecord.DecisionExecRecordReqBO;
import com.handydata.antifraud.bo.decisionexecrecord.DecisionExecRecordResBO;
import com.handydata.antifraud.common.exception.DecisionExecRecordExCode;
import com.handydata.antifraud.dao.DecisionExecRecordDao;
import com.handydata.antifraud.entity.DecisionExecRecordDO;
import com.handydata.antifraud.service.DecisionExecRecordService;
import com.handydata.common.convert.CommonBeanConvertor;
import com.handydata.common.exception.SysException;
import com.handydata.common.service.AbsService;

@Service("decisionExecRecordService")
public class DecisionExecRecordServiceImpl extends AbsService implements DecisionExecRecordService {
	
	private static final Logger logger = LoggerFactory.getLogger(DecisionExecRecordServiceImpl.class);
	
	@Autowired
	private DecisionExecRecordDao decisionExecRecordDao;

	@Transactional
	@Override
	public DecisionExecRecordResBO createDecisionExecRecord(DecisionExecRecordReqBO req) throws SysException{
	    DecisionExecRecordResBO res = new DecisionExecRecordResBO();
		try {
			DecisionExecRecordDO entity = CommonBeanConvertor.convert(req, DecisionExecRecordDO.class);
			entity.setId(UUID.randomUUID().toString().replace("-", ""));	
			long status = decisionExecRecordDao.insert(entity);
			if(status == 0) {
				res.setExCode(DecisionExecRecordExCode.SYS_NO_ENTITY_CREATED);
			} else {
				res.setExCode(DecisionExecRecordExCode.SUCCESS);
			}
 			
		} catch (Exception e) {
			logger.error("Create new entity {} get into error", req, e);
			throw new SysException(DecisionExecRecordExCode.SYS_CREATE_ENTITY_ERROR);
		}
		
		return res;
	}

	@Transactional
	@Override
	public DecisionExecRecordResBO modifyDecisionExecRecord(DecisionExecRecordReqBO req) throws SysException{
	    DecisionExecRecordResBO res = new DecisionExecRecordResBO();
		try {
			DecisionExecRecordDO condition = CommonBeanConvertor.convert(req, DecisionExecRecordDO.class);
			long status = decisionExecRecordDao.update(condition);
			if(status == 0) {
				res.setExCode(DecisionExecRecordExCode.SYS_NO_ENTITY_MODIFIED);
			} else {
				res.setExCode(DecisionExecRecordExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Modify entity {} get into error", req, e);
			throw new SysException(DecisionExecRecordExCode.SYS_MODIFY_ENTITY_ERROR);
		} 
		return res;
	}

	@Transactional
	@Override
	public DecisionExecRecordResBO deleteDecisionExecRecord(DecisionExecRecordReqBO req) throws SysException{
	    DecisionExecRecordResBO res = new DecisionExecRecordResBO();
		
		try {
			DecisionExecRecordDO condition = CommonBeanConvertor.convert(req, DecisionExecRecordDO.class);
			long status = decisionExecRecordDao.delete(condition);
			if(status == 0) {
				res.setExCode(DecisionExecRecordExCode.SYS_NO_ENTITY_DELETED);
			} else {
				res.setExCode(DecisionExecRecordExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Delete entity {} get into error", req, e);
			throw new SysException(DecisionExecRecordExCode.SYS_DELETE_ENTITY_ERROR);
		}
		return res;
	}

	@Override
	public DecisionExecRecordResBO queryDecisionExecRecord(DecisionExecRecordReqBO req) throws SysException{
		DecisionExecRecordDO condition = CommonBeanConvertor.convert(req, DecisionExecRecordDO.class);
		DecisionExecRecordResBO res;
		
		try {
			List<DecisionExecRecordDO> entities = decisionExecRecordDao.query(condition);
			res = CommonBeanConvertor.convert(entities, DecisionExecRecordResBO.class, DecisionExecRecordBeanBO.class);
		} catch(Exception e) {
			logger.error("Query entity {} get into error", req, e);
			throw new SysException(DecisionExecRecordExCode.SYS_QUERY_ENTITY_ERROR);
		}
		return res;
	}
}
